package com.ruoyi.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.graduate.domain.entity.MsPhotographyPackage;
import com.ruoyi.graduate.domain.param.MsPhotographyPackageAddParam;
import com.ruoyi.graduate.domain.param.MsPhotographyPackagePageParam;
import com.ruoyi.graduate.domain.param.MsPhotographyPackageUpdataParam;
import com.ruoyi.graduate.domain.param.PageBaseParam;
import com.ruoyi.graduate.domain.vo.MsPackageUserPageVo;
import com.ruoyi.graduate.domain.vo.MsPhotographyPackagePageVo;
import com.ruoyi.graduate.mapper.MsPhotographyPackageMapper;
import com.ruoyi.graduate.mapping.MsPhotographyPackageMapping;
import com.ruoyi.graduate.service.IMsPhotographyPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IMsPhotographyPackageServiceImpl extends ServiceImpl<MsPhotographyPackageMapper, MsPhotographyPackage>
implements IMsPhotographyPackageService {

    @Autowired
    private MsPhotographyPackageMapper msPhotographyPackageMapper;

    @Override
    public PageInfo listPage(MsPhotographyPackagePageParam param) {

        QueryWrapper qw = new QueryWrapper();
        qw.eq("photography_id", param.getPhotographyId());
        qw.like(!StringUtils.isEmpty(param.getPackageName()), "package_name", param.getPackageName());
        qw.orderByDesc("create_time");
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsPhotographyPackage> list = msPhotographyPackageMapper.selectList(qw);

        List<MsPhotographyPackagePageVo> page = MsPhotographyPackageMapping.INSTANCE.page(list);
        return new PageInfo<>(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(MsPhotographyPackageAddParam param) {

        MsPhotographyPackage add = MsPhotographyPackageMapping.INSTANCE.add(param);
        int insert = msPhotographyPackageMapper.insert(add);

        return insert > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updata(MsPhotographyPackageUpdataParam param) {

        MsPhotographyPackage updata = MsPhotographyPackageMapping.INSTANCE.updata(param);
        int i = msPhotographyPackageMapper.updateById(updata);

        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public PageInfo userPageList(PageBaseParam param) {

        PageHelper.startPage(param.getPageNum(),param.getPageSize());

        List<MsPackageUserPageVo> msPackageUserPageVos = msPhotographyPackageMapper.userPageList();

        return new PageInfo<>(msPackageUserPageVos);
    }
}
